/**
 *
 */
package kevin.study.zephyr.domain.dao.db4o;

import java.io.IOException;
import java.util.Properties;

/**
 * DB4O 的数据库配置
 * @author Kevin.Huang
 * @createtime 2007-9-26
 */
public final class DB4OConfig {

    private DB4OConfig() {
    }

    /**
     * 配置文件名
     */
    private static String configuration = "db4o.properties";

    private static Properties props = null;

    private static boolean changed = false;

    /**
     * 获取数据库路径
     * @return 数据库路径
     */
    public static String getDataPath() {
        load();
        String path = props.getProperty("db4o.path");
        throwException(path);
        return path.trim();
    }

    /**
     * @param path 配置的数据库的路径
     */
    private static void throwException(String path) {
        if (path == null) {
            throw new RuntimeException("key is not founded in " + configuration);
        }
    }

    private static void load() {
        try {
            if (props == null || changed) {
                props = new Properties();
                props.load(Resources.getResourceAsStream(configuration));
                changed = false;
            }
        } catch (IOException e) {
            // XXX 记录异常
            throw new RuntimeException("configuration: " + configuration + " is not founded!");
        }
    }

    /**
     * @param configuration the configuration to set
     */
    public static void setConfiguration(String configuration) {
        DB4OConfig.changed = true;
        DB4OConfig.configuration = configuration;
    }

}
